package com.michael.redis.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.michael.redis.object.User;
import com.michael.redis.repository.UserRepository;

@Service
@CacheConfig(cacheManager = "cacheManager")
public class UserService implements IUserService {

	@Autowired
	private UserRepository userRepository;

	@Override
	@Cacheable(value="user", cacheManager = "cacheManager")
	public User getOne(Long id) {
		return userRepository.findOne(id);
	}

	@Override
	@Cacheable(value="users", cacheManager = "cacheManager")
	public List<User> getAll() {
		return userRepository.findAll();
	}

	@Override
	@CachePut(value="user", cacheManager = "cacheManager")
	public User save(User user) {
		return userRepository.save(user);
	}

	@Override
	@CacheEvict(value="user", cacheManager = "cacheManager")
	public void del(Long id) {
		userRepository.delete(id);
	}

}
